package com.lbn.jfqsc.app.home.detail;

import com.google.gson.Gson;
import com.lbn.baseframe.mvp.BasePresenter;
import com.lbn.baseframe.net.business.RxSubscribe;
import com.lbn.baseframe.utils.L;
import com.lbn.jfqsc.App;
import com.lbn.jfqsc.business.bean.Good;
import com.lbn.jfqsc.business.bean.Goods;
import com.lbn.jfqsc.net.Host;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 李炳南
 * 2017/2/28.
 */

public class DetailPresenter extends BasePresenter<IDetailView> {



    public void getGood(final int id){

        subscribeNetwork(Host.getApi().getGood(id), new RxSubscribe<Good>() {
            @Override
            public void next(Good good) {
                getView().setGood(good);
            }

            @Override
            public void error(Throwable e) {
                getView().onError(e.getMessage());
            }
        });

        if(App.isLogin()){
            subscribeNetwork(Host.getApi().collection(App.getUser().getId(), id), new RxSubscribe<Object>() {
                @Override
                public void next(Object o) {
                    if(o!=null){
                        getView().setCollection(true);
                    }else{
                        getView().setCollection(false);
                    }
                }

                @Override
                public void error(Throwable e) {
//                    getView().onError(e.getMessage());
                }
            });
        }


    }


    public void collection(int userId,int goodsId){
        getView().showLoading();
        Map<String,Integer>map = new HashMap<>();
        map.put("userId",userId);
        map.put("goodsId",goodsId);
        subscribeNetwork(Host.getApi().collection(map), new RxSubscribe() {
            @Override
            public void next(Object o) {
                getView().hideLoading();
                getView().onError("收藏成功");
                getView().setCollection(true);
            }

            @Override
            public void error(Throwable e) {
                getView().hideLoading();
                getView().onError(e.getMessage());
                getView().setCollection(false);
            }
        });
    }


}
